package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.CategoryService;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.common.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/admin/product/category")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")

public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/findByParentId/{parentId}")
    public Result<List<Category>> findByParentId(@PathVariable Long parentId){
        List<Category> categoryList=categoryService.findByParentId(parentId);
        return Result.ok(categoryList);
    }


    /**
     * 前端传入数据的名称是file   这里要对应!不然接收不到值
     * @param file
     * @return
     */
    @PostMapping("/importData")
    public Result importData(@RequestPart("file") MultipartFile file){
        System.out.println(file);
        categoryService.importData(file);
        return Result.ok(null);
    }


    /**
     * 导出分类的文件给前端
     * @param response
     */
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        categoryService.exportData(response);
    }
}
